<template>
  <div class="gi_table_page">
    <GiTable
      row-key="id"
      title=""
      :data="dataList"
      :columns="columns"
      :loading="loading"
      :scroll="{ x: '100%', y: '100%', minWidth: 1200 }"
      :pagination="pagination"
      :disabled-tools="['size']"
      :disabled-column-keys="['title']"
      @refresh="search"
    >
      <template #toolbar-left>
        <a-input-search v-model="queryForm.title" placeholder="搜索标题" allow-clear @search="search" />
        <a-select
          v-model="queryForm.type"
          :options="notice_type"
          placeholder="请选择类型"
          allow-clear
          style="width: 150px"
          @change="search"
        />
        <a-button @click="reset">
          <template #icon><icon-refresh /></template>
          <template #default>重置</template>
        </a-button>
      </template>
      <template #toolbar-right>
        <a-button v-permission="['system:notice:create']" type="primary" @click="onAdd">
          <template #icon><icon-plus /></template>
          <template #default>新增</template>
        </a-button>
      </template>
      <template #priority="{ record }">
        <GiCellTag :value="record.priority" :dict="notice_priority_enum" />
      </template>
      <template #type="{ record }">
        <GiCellTag :value="record.type" :dict="notice_type" />
      </template>
      <template #status="{ record }">
        <GiCellTag :value="record.status" :dict="notice_status_enum" />
      </template>
      <template #action="{ record }">
        <a-space>
          <a-button v-permission="['system:notice:detail']" size="mini" title="详情" @click="onDetail(record)">详情</a-button>
          <a-button v-if="record.status === 0" v-permission="['system:notice:update']" type="primary" size="mini" @click="onUpdate(record)">
            <span>编辑</span>
          </a-button>
          <a-button v-if="record.status === 0" v-permission="['system:notice:issue']" type="primary" status="success" size="mini" @click="onIssue(record)">
            <span>发布</span>
          </a-button>
          <a-button v-if="record.status === 1" v-permission="['system:notice:issue']" type="primary" status="warning" size="mini" @click="onRevoke(record)">
            <span>撤销</span>
          </a-button>
          <a-button v-if="record.status !== 1" v-permission="['system:notice:delete']" type="primary" status="danger" size="mini" @click="onDelete(record)">
            <span>删除</span>
          </a-button>
        </a-space>
      </template>
    </GiTable>

    <NoticeAddModal ref="NoticeAddModalRef" @save-success="search" />
    <NoticeDetailModal ref="NoticeDetailModalRef" />
  </div>
</template>

<script setup lang="ts">
import type { TableInstance } from '@arco-design/web-vue'
import { Message } from '@arco-design/web-vue'
import NoticeAddModal from './NoticeAddModal.vue'
import NoticeDetailModal from './NoticeDetailModal.vue'
import { type NoticeQuery, type NoticeResp, deleteNotice, issueNotice, pageNotice, revokeNotice } from '@/apis/system'
import { useTable } from '@/hooks'
import { useDict } from '@/hooks/app'
import { isMobile } from '@/utils'
import has from '@/utils/has'

defineOptions({ name: 'SystemNotice' })

const { notice_priority_enum, notice_type, notice_status_enum } = useDict('notice_priority_enum', 'notice_type', 'notice_status_enum')

const queryForm = reactive<NoticeQuery>({
  sortField: 'createTime',
  sortOrder: 'desc'
})

const {
  tableData: dataList,
  loading,
  pagination,
  search,
  handleDelete
} = useTable((page) => pageNotice({ ...queryForm, ...page }), { immediate: true })

const columns: TableInstance[] = [
  {
    title: '序号',
    width: 66,
    align: 'center',
    render: ({ rowIndex }) => h('span', {}, rowIndex + 1 + (pagination.current - 1) * pagination.pageSize),
    fixed: !isMobile() ? 'left' : undefined
  },
  { title: '标题', dataIndex: 'title', slotName: 'title', minWidth: 200, ellipsis: true, tooltip: true },
  { title: '优先级', dataIndex: 'priority', slotName: 'priority', minWidth: 50, ellipsis: true, tooltip: true, align: 'center' },
  { title: '类型', dataIndex: 'type', slotName: 'type', align: 'center' },
  { title: '状态', dataIndex: 'status', slotName: 'status', align: 'center' },
  { title: '生效时间', dataIndex: 'effectiveTime', slotName: 'effectiveTime', width: 180 },
  { title: '终止时间', dataIndex: 'terminateTime', slotName: 'terminateTime', width: 180 },
  { title: '创建人', dataIndex: 'createBy', slotName: 'createBy', ellipsis: true, tooltip: true },
  { title: '创建时间', dataIndex: 'createTime', slotName: 'createTime', width: 180 },
  {
    title: '操作',
    dataIndex: 'action',
    slotName: 'action',
    width: 240,
    align: 'center',
    fixed: !isMobile() ? 'right' : undefined,
    show: has.hasPermOr(['system:notice:view', 'system:notice:update', 'system:notice:delete'])
  }
]

// 重置
const reset = () => {
  queryForm.title = undefined
  queryForm.type = undefined
  search()
}

// 删除
const onDelete = (record: NoticeResp) => {
  return handleDelete(() => deleteNotice(record.id), {
    content: `是否确定删除公告「${record.title}」？`,
    showModal: true
  })
}

const NoticeAddModalRef = ref<InstanceType<typeof NoticeAddModal>>()
// 新增
const onAdd = () => {
  NoticeAddModalRef.value?.onAdd()
}

// 修改
const onUpdate = (record: NoticeResp) => {
  NoticeAddModalRef.value?.onUpdate(record.id)
}

const NoticeDetailModalRef = ref<InstanceType<typeof NoticeDetailModal>>()
// 详情
const onDetail = (record: NoticeResp) => {
  NoticeDetailModalRef.value?.onDetail(record.id)
}

// 发布
const onIssue = (record: NoticeResp) => {
  issueNotice(record.id)
  Message.success('发布成功')
  search()
}

// 撤销
const onRevoke = (record: NoticeResp) => {
  revokeNotice(record.id)
  Message.success('撤销成功')
  search()
}
</script>

<style scoped lang="scss"></style>
